// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <optional>
#include <pollux/functions/sparksql/tests/SparkFunctionBaseTest.h>

using namespace kumo::pollux::test;

namespace kumo::pollux::functions::sparksql::test {
namespace {

class JsonArrayLengthTest : public SparkFunctionBaseTest {
 protected:
  const auto jsonArrayLength(const std::string& json) {
    auto varcharVector = make_flat_vector<std::string>({json});
    return evaluateOnce<int32_t>(
        "json_array_length(c0)", make_row_vector({varcharVector}));
  }
};

TEST_F(JsonArrayLengthTest, basic) {
  EXPECT_EQ(4, jsonArrayLength(R"([1,2,3,4])"));
  EXPECT_EQ(5, jsonArrayLength(R"([1,2,3,{"f1":1,"f2":[5,6]},4])"));
  // Malformed Json.
  EXPECT_EQ(std::nullopt, jsonArrayLength(R"([1,2)"));
}

} // namespace
} // namespace kumo::pollux::functions::sparksql::test
